﻿#pragma once
#include<iostream>
#include<vector>
#include<assert.h>
using namespace std;
template<class K,class V>
struct AVLTreeNode
{   
	AVLTreeNode(const pair<K, V>& pair)
		:_pair(pair)
		, _left(nullptr)
		, _right(nullptr)
		, _parent(nullptr)
		, _bf(0)
	{}
	pair<K, V> _pair;
	AVLTreeNode<K, V>* _left;
	AVLTreeNode<K, V>* _right;
	AVLTreeNode<K, V>* _parent;
	//平衡因子;
	int _bf;
};
template<class K,class V>
class AVLTree
{
public:
	using Node = AVLTreeNode<K, V>;
	bool Insert(const pair<K, V>& pair)
	{
		Node* cur = _root;
		Node* parent = nullptr;
		while (cur)
		{
			if (pair.first < cur->_pair.first)
			{
				parent = cur;
				cur = cur->_left;
			}
			else if (pair.first > cur->_pair.first)
			{
				parent = cur;
				cur = cur->_right;
			}
			else
			{
				return false;
			}
		}
		//插入;
		cur = new Node(pair);
		if (parent == nullptr)
		{
			 _root=cur;
		}
		else if (cur->_pair.first < parent->_pair.first)
		{
			parent->_left = cur;
		}
		else
		{
			parent->_right = cur;
		}
		//更新父节点；
		cur->_parent = parent;
		//更新平衡因子;
		while (parent)
		{
			if (parent->_left == cur)
			{
				parent->_bf--;
			}
			else
			{
				parent->_bf++;
			}

			if (parent->_bf == 0)
			{
				break;
			}
			else if (parent->_bf == -1 || parent->_bf == 1)
			{
				cur = parent;
				parent = parent->_parent;
			}
			else if (parent->_bf == -2 || parent->_bf == 2)
			{
				//不平衡了，需要旋转调整;
				Node* pLeft = parent->_left;
				Node* pRight = parent->_right;
				//右单旋
				if (parent->_bf == -2 && pLeft->_bf == -1)
				{
					RotateR(parent);
				}
				//左单旋;
				else if (parent->_bf == 2 && pRight->_bf == 1)
				{
					RotateL(parent);
				}
				//左右旋转
				else if (parent->_bf == -2 && pLeft->_bf == 1)
				{
					RotateLR(parent);
				}
				//右左旋转
				else if (parent->_bf == 2 && pRight->_bf == -1)
				{
					RotateRL(parent);
				}
				else
				{
					assert(false);
				}
				break;
			}
			//检查程序是否出错;
			else
			{
				assert(false);
			}
		}
		return true;
	}
	void RotateR(Node*parent)
	{
		Node* pLeft = parent->_left;
		Node* pLeftR = pLeft->_right;
		//记录父亲的父亲节点，以便后面连接;
		Node* pParent = parent->_parent;
		pLeft->_right = parent;
		parent->_left = pLeftR;
		if (pLeftR)
		{
			pLeftR->_parent = parent;
		}
		pLeft->_parent = pParent;
		parent->_parent = pLeft;
		if (pParent)
		{  
			if (pParent->_left == parent)
			{
				pParent->_left = pLeft;
			}
			else
			{
				pParent->_right = pLeft;
			}
		}
		else
		{
			_root=pLeft;
		}
		//更新平衡因子;
		parent->_bf = pLeft->_bf = 0;
	}
	void RotateL(Node* parent)
	{
		Node* pRight = parent->_right;
		Node* pRightL = pRight->_left;
		Node* pParent = parent->_parent;
		pRight->_left = parent;
		pRight->_parent = pParent;
		parent->_right = pRightL;
		if (pRightL)
		{
			pRightL->_parent = parent;
		}
		parent->_parent = pRight;
		if (pParent == nullptr)
		{
			_root = pRight;
		}
		else
		{
			if (pParent->_left == parent)
			{
				pParent->_left = pRight;
			}
			else
			{
				pParent->_right = pRight;
			}
		}
		parent->_bf = pRight->_bf = 0;
	}
	void RotateLR(Node* parent)
	{
		Node* pLeft = parent->_left;
		Node* pLeftR = pLeft->_right;
		int bf = pLeftR->_bf;
		RotateL(parent->_left);
		RotateR(parent);
		if (bf == 0)
		{
			pLeft->_bf = 0;
			pLeftR->_bf = 0;
			parent->_bf = 0;
		}
		else if (bf == 1)
		{
			pLeft->_bf = -1;
			pLeftR->_bf = 0;
			parent->_bf = 0;
		}
		else if (bf == -1)
		{
			pLeft->_bf = 0;
			pLeftR->_bf = 0;
			parent->_bf = 1;
		}
	}
	void RotateRL(Node* parent)
	{
		Node* pRight = parent->_right;
		Node* pRightL = pRight->_left;
		int bf = pRightL->_bf;
		RotateR(parent->_right);
		RotateL(parent);
		if (bf == 0)
		{
			pRight->_bf = 0;
			pRightL->_bf = 0;
			parent->_bf = 0;
		}
		else if (bf == 1)
		{
			pRight->_bf = 0;
			pRightL->_bf = 0;
			parent->_bf = -1;
		}
		else if (bf == -1)
		{  
			pRight->_bf = 1;
			pRightL->_bf = 0;
			parent->_bf = 0;

		}
	}
	void InOrder()
	{
		_InOrder(_root);
		cout << endl;
	}
	int Height()
	{
		return _Height(_root);
	}
	bool IsBalanceTree()
	{
		return _IsBalanceTree(_root);
	}
private:
	bool _IsBalanceTree(Node* root)
	{
		// 空树也是AVL树
		if (nullptr == root)
			return true;
		// 计算pRoot结点的平衡因子：即pRoot左右子树的高度差
		int leftHeight = _Height(root->_left);
		int rightHeight = _Height(root->_right);
		int diff = rightHeight - leftHeight;

		// 如果计算出的平衡因子与pRoot的平衡因子不相等，或者
		// pRoot平衡因子的绝对值超过1，则一定不是AVL树
		if (abs(diff) >= 2)
		{
			cout << root->_pair.first << "高度差异常" << endl;
			return false;
		}

		if (root->_bf != diff)
		{
			cout << root->_pair.first << "平衡因子异常" << endl;
			return false;
		}

		// pRoot的左和右如果都是AVL树，则该树一定是AVL树
		return _IsBalanceTree(root->_left) && _IsBalanceTree(root->_right);
	}
	int _Height(Node* root)
	{
		if (root == nullptr)
			return 0;
		int leftHeight = _Height(root->_left);
		int rightHeight = _Height(root->_right);
		return leftHeight > rightHeight ? leftHeight + 1 : rightHeight + 1;
	}

	void _InOrder(Node* root)
	{
		if (root == nullptr)
		{
			return;
		}
		_InOrder(root->_left);
		cout << root->_pair.first << " " << root->_pair.second << " ";
		_InOrder(root->_right);
	}
	Node* _root=nullptr;
};
